package com.wifi.channerClass.dao;

import com.sun.deploy.uitoolkit.impl.awt.ui.SwingConsoleWindow;
import com.wifi.channerClass.entity.ChannelClassEntity;
import com.wifi.unti.DBHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

import static java.time.LocalDate.now;

public class ChannelClassDaoImp implements  ChannelClassDao {


    @Override
    public List<Object> select(Integer pageNum, Integer pageSize) throws SQLException {
        String  sql="SELECT * FROM `shenzhou`.`tb_channel_class` LIMIT ?,?";
        List<Object> list = DBHelper.selectAll(sql, new ChannelClassEntity(), pageNum, pageSize);
        System.out.println(list);
        return list;
    }

    @Override
    public long selectCount(String pid) throws SQLException {

        QueryRunner runner= new QueryRunner(DBHelper.getDataSource());
        StringBuffer stringBuffer=new StringBuffer("SELECT COUNT(*) FROM `shenzhou`.`tb_menu`");
        if (!pid.isEmpty()){
            stringBuffer.append("where pid=");
            stringBuffer.append(pid);
        }
        String sql=stringBuffer.toString();
        System.out.println(sql);
        long count = runner.query(sql,new ScalarHandler<Long>());
        return count;
    }

    @Override
    public List<Object> selectData(Integer pageNum, Integer pageSize, String pid) throws SQLException {

        StringBuffer stringBuffer=new StringBuffer("SELECT m.*,n.name fatherName FROM `shenzhou`.`tb_channel_class` m left join `shenzhou`.`tb_channel_class` n on m.pid=n.id  ");
        if (!pid.isEmpty()){
            stringBuffer.append("where m.pid=");
            stringBuffer.append(pid);
        }
        stringBuffer.append(" LIMIT ?,?");
        String sql=stringBuffer.toString();
        System.out.println(sql);
//        String sql="SELECT m.*,n.name fatherName FROM `shenzhou`.`tb_menu` m left join `shenzhou`.`tb_menu` n on m.pid=n.id LIMIT ?,?";
        return DBHelper.selectAll(sql,new ChannelClassEntity(),pageNum,pageSize);
    }

    @Override
    public List<Object> selectFather() throws SQLException {
        String sql="SELECT * FROM `shenzhou`.`tb_channel_class`  where pid=0;";
        return DBHelper.selectAll(sql,new ChannelClassEntity());
    }

    @Override
    public long insert(String name, int pid) throws SQLException {
        String sql="INSERT INTO `shenzhou`.`tb_channel_class`(`name`, `pid`, `status`, `creatTime`) VALUES (?,?,0,now())";
        Long insert = DBHelper.insert(sql, name, pid);
        System.out.println( insert);
        return insert;
    }

    @Override
    public long remove(int id, int s) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_channel_class` SET `status` = ? WHERE `id` = ?";
        long update = DBHelper.update(sql, s,id);
        System.out.println(update);
        return  update;
    }

    @Override
    public Object selectById(int id) throws SQLException {
        String sql="SELECT * FROM `shenzhou`.`tb_channel_class` where id =?";
        return DBHelper.select(sql,new ChannelClassEntity(),id);
    }

    @Override
    public long edit(int id, String name, int pid) throws SQLException {
       String sql="UPDATE `shenzhou`.`tb_channel_class` SET `name` = ?, `pid` = ? WHERE `id` = ?";
        long update = DBHelper.update(sql, name,pid,id);
        System.out.println(update);
        return 0;
    }

    @Override
    public int editByname(ChannelClassEntity channelClassEntity) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_channel_class` SET `name` = ?, `pid` = ?, `status` =? WHERE `id` = ?";
        return DBHelper.update(sql,channelClassEntity.getName(),channelClassEntity.getPid(),channelClassEntity.getStatus(),channelClassEntity.getId());
    }


}
